package com.itbour.service.material.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
//import com.itbour.dao.material.DocDao;
import com.itbour.dao.material.TmplDao;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.template.DocTypeDto;
//import com.itbour.dto.template.DocumentInfoDto;
//import com.itbour.dto.template.PageInfoDto;
import com.itbour.dto.template.TemplateInfoDto;
import com.itbour.dto.template.TmplPageInfoDto;
import com.itbour.dto.template.TmplUserDocDto;
import com.itbour.dto.tg.KeywordInfoDto;
import com.itbour.dto.trade.TmplPriceInfoDto;
import com.itbour.service.material.TmplService;
import com.opensymphony.xwork2.ActionContext;


/**
*
* copyright   itbour team 2015
* @author     xc
*
*/

public class TmplServiceImpl implements TmplService {

	private TmplDao tmplDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = tmplDao.selectTmplCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<TemplateInfoDto> data = tmplDao.selectTmplInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	public Page pageQueryTmplPrice(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = tmplDao.selectTmplPriceCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<TmplPriceInfoDto> data = tmplDao.selectTmplPriceInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<TemplateInfoDto> selectTmplInfo(Map<String,Object> param) {
		return tmplDao.selectTmplInfo(param);
	}
	
	public List<TemplateInfoDto> selectMstTmplLst(Map<String,Object> param) {
		return tmplDao.selectMstTmplLst(param);
	}
	
	public List<TmplPageInfoDto> selectTmplPage(Map<String,Object> param) {
		return tmplDao.selectTmplPageInfo(param);
	}
	public TmplPageInfoDto chkTmplPageInfo(Map<String,Object> param) {
		return tmplDao.chkTmplPageInfo(param);
	}
	
	public void deleteTmplInfo(String ids) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(ids)));
		tmplDao.deleteTmplLogic(param);
		tmplDao.deleteTmplPageLogic(param);
	}
	
	public void deleteTmplPage(Map<String,Object> param) {
		tmplDao.deleteTmplPageLogic(param);
	}

	public void addTmplDoc(TemplateInfoDto docInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("base_tmpl_id", docInfo.getBase_tmpl_id());
		param.put("tmpl_name", docInfo.getTmpl_name());
		param.put("master", docInfo.getMaster());
		param.put("user_id", docInfo.getUser_id());
		param.put("user_type", docInfo.getUser_type());
		param.put("publish", docInfo.getPublish());
		param.put("data", docInfo.getData());
		param.put("ver", docInfo.getVer());
		param.put("update_by", docInfo.getUpdate_by());
		tmplDao.insertTmplInfo(param);
	}
	
	public void addTmplPage(TmplPageInfoDto pageInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tmpl_id", pageInfo.getTmpl_id());
		param.put("base_page_id", pageInfo.getBase_page_id());
		param.put("page_index", pageInfo.getPage_index());
		param.put("user_type", pageInfo.getUser_type());
		param.put("backup_flg", pageInfo.getBackup_flg());
		param.put("data", pageInfo.getData());
		param.put("replaces", pageInfo.getReplaces());
		param.put("rep_hash_tag", pageInfo.getRep_hash_tag());
		param.put("thumb_s", pageInfo.getThumb_s());
		param.put("thumb_m", pageInfo.getThumb_m());
		param.put("thumb_l", pageInfo.getThumb_l());
		param.put("update_by", pageInfo.getUpdate_by());
		tmplDao.insertTmplPageInfo(param);
	}

	public void updateTmplDocById(Map<String,Object> param) {
		tmplDao.updateTmplInfoById(param);
	}
	
	public void updateTmplPageById (Map<String,Object> param) {
		tmplDao.updateTmplPageInfoById(param);
	}

	public TemplateInfoDto getLatestTmplDoc() {
		return tmplDao.selectLatestTmplInfo();
	}
	
	public List<String> getTmplTypeIds(String docId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("doc_id", docId);
		return tmplDao.selectTmplTypeIds(param);
	}
	// 更新文档分类
	public void updateTmplTypeInfo(String docId, String typeIds) {
		// 删除旧数据
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("doc_id", docId);
		tmplDao.deleteTmplTypeById(param);
		
		// 新规
		List<Long> typeList = StringUtils.stringToList2(typeIds);
		if (typeList != null && typeList.size() > 0) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	param.put("update_by", user.getUser_name());
	    	
			for (int i = 0; i < typeList.size(); i++) {
				param.put("type_id", typeList.get(i));
				tmplDao.insertTmplTypeInfo(param);
			}
		}
	}
	
	// 检索关键词
	public List<KeywordInfoDto> selectDocKeywordInfo(Map<String,Object> param){
		return tmplDao.selectDocKeywordInfo(param);
	}
	
	public List<String> findTmplIdsByTypeIds(Map<String,Object> param) {
		return tmplDao.selectTmplIdsByTypeIds(param);
	}
	
	public List<DocTypeDto> getDocTypeForCombo(Map<String,Object> param) {
		return tmplDao.selectTmplType(param);
	}
	
	// 模版价格增删改
	public void insertTmplPrice(Map<String,Object> param){
		tmplDao.insertTmplPrice(param);
	}
	public void deleteTmplPriceInfo(Map<String,Object> param){
		tmplDao.deleteTmplPriceLogic(param);
	}
	public void updateTmplPriceById(Map<String,Object> param){
		tmplDao.updateTmplPriceById(param);
	}

	public TmplDao getTmplDao() {
		return tmplDao;
	}

	public void setTmplDao(TmplDao tmplDao) {
		this.tmplDao = tmplDao;
	}

	//
	public Page getTmplUserDocInfo(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = tmplDao.getTmplDocCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<TmplUserDocDto> data = tmplDao.getTmplrDocInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

}
